﻿using System.Web;
using System.Web.SessionState;
using CustomerContactSystem.Core.Infrastructure.Configuration;
using CustomerContactSystem.Core.Infrastructure.DataAccess;
using CustomerContactSystem.Core.Model;
using StructureMap;

namespace CustomerContactSystem.WebForms
{
    /// <summary>
    /// Summary description for AjaxService
    /// </summary>
    public class AjaxService : IHttpHandler, IRequiresSessionState {
        public void ProcessRequest(HttpContext context) {
            var settings = ObjectFactory.GetInstance<IApplicationSettings>();
            var userRepository = ObjectFactory.GetInstance<INhibernateRepository<User>>();

            var userId = 0;

            if (context.Session[settings.UserSessionKey] != null) {
                userId = (int)context.Session[settings.UserSessionKey];
            }

            if (userId == 0) {
                return;
            }

            var user = userRepository.GetById(userId);

            var queryType = "";
            if (context.Request.QueryString["q"] != null) {
                queryType = context.Request.QueryString["q"];
            }

            if (queryType.Length <= 0) {
                return;
            }

            switch (queryType) {
                case "access":
                    CheckUsersAccesToEnquiry(context, user);
                    break;
            }
        }

        private void CheckUsersAccesToEnquiry(HttpContext ctx, User user) {
            var enquiryRepository = ObjectFactory.GetInstance<INhibernateRepository<Enquiry>>();
            string output;
            var enquiryId = 0;

            if (ctx.Request.QueryString["id"] != null) {
                enquiryId = int.Parse(ctx.Request.QueryString["id"]);
            }

            if (enquiryId == 0) {
                output = "Invalid Enquiry Id!";
            }
            else {
                var enquiry = enquiryRepository.GetById(enquiryId);
                var currentReferral = enquiry.CurrentReferral;
                var currentReferralUserId = currentReferral == null ? 0 : currentReferral.CurrentlyReferredTo.Id;
                var currentReferralGroupId = currentReferral == null ? 0 : currentReferral.CurrentlyReferredToGroup.Id;
                var currentReferralDepartmentId = currentReferral == null ? 0 : currentReferral.CurrentlyReferredToDepartment.Id;

                output = user.IsSubmitterRefereeOrAdminOver(enquiry.SubmittedBy.Id, enquiry.SubmittedByGroup.Id,
                                                            enquiry.SubmittedByDepartment.Id, currentReferralUserId,
                                                            currentReferralGroupId, currentReferralDepartmentId)
                             ? "yes"
                             : "no";
            }

            ctx.Response.ContentType = "text/plain";
            ctx.Response.Write(output);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}